package com.example.farmer.Database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.farmer.Entity.ProductPost;
import com.example.farmer.Entity.PurchasePost;
import com.example.farmer.Presenter.PurchasePostPresenter;

import java.util.ArrayList;
import java.util.List;

public class PurchasePublishDatabase extends SQLiteOpenHelper {
    private static final String[] COLUMN_NAMES = {"id","product_type","product_name","need",
            "period"};
    private static PurchasePublishDatabase instance;
    private static final String DATABASE_NAME = "farmer_purchase.db"; // 数据库名称
    private static final int DATABASE_VERSION = 1; // 数据库版本

    // 表名和列名
    private static final String TABLE_NAME = "purchase_posts";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_PUSH_TYPE = "push_type";
    private static final String COLUMN_PRODUCT_TYPE = "product_type";
    private static final String COLUMN_PRODUCT_NAME = "product_name";
    private static final String COLUMN_NEED = "need";
    private static final String COLUMN_SHIP_ADDRESS = "ship_address";
    private static final String COLUMN_PERIOD = "period";
    private static final String COLUMN_SHIPPING_ADDRESS = "shipping_address";
    private static final String COLUMN_DESCRIBE = "describe";
    private static final String COLUMN_NUMBER = "userNumber";


    // 创建表的 SQL 语句
    private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_PUSH_TYPE + " TEXT,"
            + COLUMN_PRODUCT_TYPE + " TEXT,"
            + COLUMN_PRODUCT_NAME + " TEXT,"
            + COLUMN_NEED + " TEXT,"
            + COLUMN_SHIP_ADDRESS + " TEXT,"
            + COLUMN_PERIOD + " TEXT,"
            + COLUMN_SHIPPING_ADDRESS + " TEXT,"
            + COLUMN_DESCRIBE + " TEXT,"
            + COLUMN_NUMBER + " TEXT"
            + ")";

    public PurchasePublishDatabase(Context context) {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    public static synchronized PurchasePublishDatabase getInstance(Context context) {
        if (instance == null) {
            instance = new PurchasePublishDatabase(context);
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 当数据库版本变化时，删除旧表并创建新表
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    // 数据库操作方法，如insert, update, delete, query等

    //根据发布类型查询
    public List<PurchasePost> getPostsByPushType(String pushType) {
        List<PurchasePost> posts = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, COLUMN_NAMES, COLUMN_PUSH_TYPE + " = ?", new String[]{pushType}, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                // 假设ClassPost类有相应的构造函数或者setter方法来填充数据
                @SuppressLint("Range") PurchasePost post = new PurchasePost(
                        cursor.getInt(cursor.getColumnIndex(COLUMN_ID)),
                        cursor.getString(cursor.getColumnIndex(COLUMN_PRODUCT_NAME)),
                        cursor.getString(cursor.getColumnIndex(COLUMN_PRODUCT_TYPE)),
                        cursor.getString(cursor.getColumnIndex(COLUMN_NEED)),
                        cursor.getString(cursor.getColumnIndex(COLUMN_PERIOD))

                        // 这里没有包含所有字段，根据ClassPost类的定义添加
                );
                posts.add(post);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        return posts;
    }
    // 根据货品类型查询数据库并返回商品列表
    @SuppressLint("Range")
    public List<PurchasePost> getPostsByType(String producttype) {
        // 查询特定类型的帖子列表
        List<PurchasePost> purchasePostList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();
        try (Cursor cursor = db.query( // 执行查询
                TABLE_NAME, // 表名
                COLUMN_NAMES, // 列名数组
                COLUMN_PRODUCT_TYPE + " = ?", // 查询条件，假设有一个列名为 "type"
                new String[]{producttype}, // 查询条件的参数
                null, // GROUP BY
                null, // HAVING
                null // ORDER BY
        )) {
            while (cursor.moveToNext()) {
                // 遍历查询结果
                int id = cursor.getInt(cursor.getColumnIndex(COLUMN_ID));

//                String producttype = cursor.getString(cursor.getColumnIndex(COLUMN_PRODUCT_TYPE));
                String productname = cursor.getString(cursor.getColumnIndex(COLUMN_PRODUCT_NAME));
                String need = cursor.getString(cursor.getColumnIndex(COLUMN_NEED));
                String shipaddress = cursor.getString(cursor.getColumnIndex(COLUMN_SHIP_ADDRESS));
                String period = cursor.getString(cursor.getColumnIndex(COLUMN_PERIOD));
                String shippingaddress = cursor.getString(cursor.getColumnIndex(COLUMN_SHIPPING_ADDRESS));
                String describe = cursor.getString(cursor.getColumnIndex(COLUMN_DESCRIBE));
                // 创建 ClassPost 对象并添加到列表中
                purchasePostList.add(new PurchasePost(id,productname,need,shipaddress,
                        period,shippingaddress, describe));
            }
            db.close();
        }
        return purchasePostList;
    }
    //插入数据
    public long insertPost(String pushtype,String producttype,String productname,  String need, String shipaddress,
                           String period,String shippingaddress,String describe) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_PUSH_TYPE,pushtype);
        values.put(COLUMN_PRODUCT_TYPE,producttype);
        values.put(COLUMN_PRODUCT_NAME, productname);
        values.put(COLUMN_NEED,need);
        values.put(COLUMN_SHIP_ADDRESS,shipaddress);
        values.put(COLUMN_PERIOD,period);
        values.put(COLUMN_SHIPPING_ADDRESS,shippingaddress);
        values.put(COLUMN_DESCRIBE,describe);

        // 插入数据并返回操作成功与否
        long result = db.insert(TABLE_NAME, null, values);
        return result;
    }


    //删除数据
    public void deletePost(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, COLUMN_ID + " = ?", new String[]{String.valueOf(id)});
    }
}
